﻿#ifndef MAINHOLDWIDGET_H
#define MAINHOLDWIDGET_H

#include <QWidget>
#include <qstandarditemmodel.h>
#include <qabstractitemmodel.h>
#include <string.h>
#include <qtableview.h>
#include <qheaderview.h>
#include <qgridlayout.h>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QPushButton>
#include <QMenu>
#include <QAction>
#include<QEvent>
#include<QMutex>
#include"CtpInterface/ThostFtdcUserApiStruct.h"

class InvestorPosition{
public:
    InvestorPosition(QString futu_code,char entrust_bs,int begin_amount,int enable_amount,int real_enable_amount,int real_current_amount,int old_current_amount);
    int GetRealEnableAmount(){ return m_real_enable_amount;}
private:
    QString m_futu_code;	//合约代码
    char m_entrust_bs;	//买卖方向
    int m_begin_amount;//期初数量
    int m_enable_amount;//可用数量
    int m_real_enable_amount;//当日开仓可用数量

    int m_real_current_amount;//今总持仓
    int m_old_current_amount;//老仓持仓数量
};

class MainHoldWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MainHoldWidget(QWidget *parent = 0);

    bool eventFilter(QObject *obj, QEvent *event);

    void AddRow(CThostFtdcInvestorPositionField *pInvestorPosition,int amountPerHand);

    int GetRealEnableAmount(QString futucode,char entrust_bs);

    //bool GetInstrumentMarginRateFutucodeFlg();
    //bool GetContractInfoFlg();

    //根据行情信息更新持仓损益
    void UpdateByDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);

signals:
    void SigkeyPressEvent(QKeyEvent* k);

    void SigClosePositon(QString exch_type,QString contract_code, char c_bs, char c_direction, char c_hedge,int real_enable_amount,int enable_amount);


public slots:
    void QueryButtonClicked();

    void TableDoubleClicked(const QModelIndex &index);

	//右键弹出菜单响应函数
	void RightClickedMenuPop(const QPoint& pos);
	//市价平仓事件绑定
	void ShijiaAction();
	//对价平仓事件绑定
	void DuijiaAction();
	//刷新事件绑定
	void UpdateHold();
	//导出事件绑定
	void OutputHold();

    //当收到成交回报时 更新用户的持仓信息
    void UpdateHold(CThostFtdcTradeField *pTrade);

public:
    void setupModel();
    void setupViews();
    //void ShowInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition);


public:
    QTableView*         m_tableHold;
    QAbstractItemModel* m_modelHold;
    QComboBox*          m_accountCombox;
	//保存持仓信息
    QMap<QString,InvestorPosition*> m_LongPositions;
    QMap<QString,InvestorPosition*> m_ShortPositions;
    QMap<QString,int> m_MapAmountPerHand;

private:
    QMutex              m_UpdateMutex;
    //int                 m_InstrumentMarginRateFutucode;
    //int                 m_ContractInfo;

    QPushButton*		m_querybutton;
    QPushButton*		m_exportbutton;
	QMenu*				m_RightPopMenu;
	QAction*			m_ShijiaAction; 
	QAction*			m_DuijiaAction; 
	QAction*			m_updateAction;
	QAction*			m_outputAction; 

    QItemSelectionModel *selectionModel;
    QGridLayout *MainLayout;
    QHBoxLayout *hboxlayout;
};

#endif // MAINHOLDWIDGET_H
